import { Util } from '../utils';
import { isArray } from 'lodash';

/**
 * @desc 图层类
 */
class LFTLayer {
  constructor(id, viewer) {
    this._id = id;
    this._viewer = viewer;
    this._overlays = {};
    this._groupId = null;
    this._map = viewer._map;
    this._L = viewer._L;
    // 图层组容器
    this._layerMap = viewer._L.layerGroup();
    // 要素容器
    this._featureMap = viewer._L.featureGroup();

    // this._canvasMap = viewer._L
    //   .canvasMarkerLayer({ collisionFlg: false })
    //   .addTo(viewer._map);
  }

  get id() {
    return this._id;
  }

  get viewer() {
    return this._viewer;
  }

  get groupId() {
    return this._groupId;
  }
  set id(id) {
    this._id = id;
  }
  set groupId(groupId) {
    this._groupId = groupId;
  }

  set viewer(viewer) {
    this._viewer = viewer;
  }

  // 通过覆盖物id获取覆盖物
  getOverlay(id) {
    return this._overlays[id] || undefined;
  }

  // 通过ids获取所有覆盖物
  getOverlays(ids) {
    if (ids) {
      let _arr = [];
      let _overlays = {};
      if (typeof ids === 'string') {
        _arr = ids.split(',');
      } else if (Array.isArray(ids)) {
        _arr = ids;
      }
      _arr.forEach((m) => {
        if (this._overlays[m]) {
          _overlays[m] = this._overlays[m];
        }
      });
      return _overlays;
    } else {
      return this._overlays;
    }
  }

  // 通过ids隐藏覆盖物
  hideOverlays(ids) {
    let overlays = this.getOverlays(ids);
    if (overlays) {
      if (!Array.isArray(overlays)) {
        overlays = Object.values(overlays);
      }
      overlays.forEach((m) => {
        m.hide && m.hide();
      });
    }
  }

  // 通过ids显示覆盖物
  showOverlays(ids) {
    let overlays = this.getOverlays(ids);
    if (overlays) {
      if (!Array.isArray(overlays)) {
        overlays = Object.values(overlays);
      }
      overlays.forEach((m) => {
        m.show && m.show();
      });
    }
  }

  // 添加覆盖物
  addOverlay(overlay) {
    if (!Object(this._overlays)[overlay?.id]) {
      this._overlays[overlay.id] = overlay;
      overlay.parent = this;
      overlay?._onAdd && overlay._onAdd(this);
      // 添加覆盖物到地图上
      // console.log('overlay', overlay?.overlayType);
      // 判断是否是canvas绘制的大量目标
      if (overlay?.overlayType === 'CanvasMarker') {
        // 此处用于绘制大目标量数据
        if (overlay._lft) {
          // this._canvasMap.addLayer(overlay._lft, true);
        }
      } else {
        if (overlay._lft) {
          this._layerMap.addLayer(overlay._lft).addTo(this._map);
        }
        if (overlay._lftLabel) {
          this._layerMap.addLayer(overlay._lftLabel).addTo(this._map);
        }
      }
    }
    return this;
  }

  // 移除覆盖物
  removeOverlay(id) {
    if (Object(this._overlays).hasOwnProperty(id)) {
      let overlay = this._overlays[id];
      if (overlay) {
        // 从地图中删除
        if (overlay?.overlayType === 'CanvasMarker') {
          // overlay._lft && this._canvasMap.removeLayer(overlay._lft);
        } else {
          overlay._lft && this._layerMap.removeLayer(overlay._lft);
        }
        overlay._lftLabel && this._layerMap.removeLayer(overlay._lftLabel);
        // this._layerMap.clearLayers();
      }
      // 从数据结构中删除
      delete this._overlays[id];
    }
  }
  // 清除覆盖物
  clear() {
    if (this._overlays && Object.keys(this._overlays).length > 0) {
      let overlayArr = Object.keys(this._overlays);
      // console.log('overlayArr', overlayArr);
      overlayArr.map((i) => {
        // console.log('进来清除了', i, this._overlays[i]._lft, this._layerMap);
        if (this._overlays[i]._lft) {
          // 清除地图上绘制的东西
          if (this._overlays[i].overlayType === 'CanvasMarker') {
            // this._canvasMap.removeLayer(this._overlays[i]._lft);
          } else {
            this._layerMap.removeLayer(this._overlays[i]._lft);
          }

          // this._layerMap.clearLayers();
        }
        this._overlays[i]._lftLabel &&
          this._layerMap.removeLayer(this._overlays[i]._lftLabel);
      });
    }
    this._overlays = {};
    // 还需要清除groups里面的数据
    // if (this._groupId) {
    //   let tmpgroup = this._viewer._groups[this._groupId];
    //   let tmplayers = tmpgroup._layers;
    //   // console.log('进来清理grouple', tmpgroup, tmplayers, this._id)
    //   if (tmpgroup && tmplayers) {
    //     if (Object(tmplayers).hasOwnProperty(this._id)) {
    //       tmplayers[this._id] = {};
    //     }
    //   }
    // }
  }
  // 通过id清除图层
  removeLayer(id) {
    if (Object(this._viewer._layers).hasOwnProperty(id)) {
      let layer = this._viewer._layers[id];
      if (layer) {
        delete this._viewer._layers[id];
      }
    }
  }

  // 清空图层
  remove() {
    this.clear();
  }
}

export default LFTLayer;
